package com.esri.core.geometry;

import com.esri.core.geometry.RasterizedGeometry2D;
import com.github.mikephil.charting.utils.Utils;

/* loaded from: classes.dex */
class PointInPolygonHelper {
    private Point2D a;
    private double e;
    private double f;
    private double g;
    private double h;
    private boolean i;
    private boolean j;
    private boolean k;
    private boolean l;
    private SegmentBuffer[] c = null;
    private double[] d = null;
    private int b = 0;

    private int a() {
        return this.b != 0 ? 1 : 0;
    }

    private boolean a(Segment segment) {
        return Point2D.b(segment.d(segment.a(this.a, false)), this.a) <= this.f;
    }

    private void b(Segment segment) {
        if (!this.j && ((this.i && this.a.isEqual(segment.a())) || this.a.isEqual(segment.j()))) {
            this.k = true;
        } else if (segment.getStartY() == this.a.y && segment.getStartY() == segment.getEndY()) {
            if (!this.i || this.j) {
                return;
            }
            double min = Math.min(segment.getStartX(), segment.getEndX());
            double max = Math.max(segment.getStartX(), segment.getEndX());
            if (this.a.x <= min || this.a.x >= max) {
                return;
            }
            this.k = true;
        } else {
            boolean z = false;
            if (this.a.x > Math.max(segment.getStartX(), segment.getEndX()) || (this.a.x >= Math.min(segment.getStartX(), segment.getEndX()) && segment.intersectionWithAxis2D(true, this.a.y, this.d, null) > 0 && this.d[0] <= this.a.x)) {
                z = true;
            }
            if (z) {
                if (this.a.y == segment.a().y) {
                    if (this.a.y < segment.j().y) {
                        return;
                    }
                } else if (this.a.y == segment.j().y && this.a.y < segment.a().y) {
                    return;
                }
                if (this.i) {
                    this.b ^= 1;
                } else {
                    this.b += segment.a().y <= segment.j().y ? -1 : 1;
                }
            }
        }
    }

    public PointInPolygonHelper(boolean z, Point2D point2D, double d) {
        this.a = point2D;
        this.g = point2D.y - d;
        this.h = point2D.y + d;
        this.i = z;
        this.e = d;
        this.f = d * d;
        this.j = d != Utils.DOUBLE_EPSILON;
        this.k = false;
    }

    private boolean c(Segment segment) {
        Envelope1D queryInterval = segment.queryInterval(0, 1);
        if (queryInterval.vmin <= this.h && queryInterval.vmax >= this.g) {
            if (this.j && a(segment)) {
                return true;
            }
            if (queryInterval.vmin <= this.a.y && queryInterval.vmax >= this.a.y) {
                if (this.c == null) {
                    this.c = new SegmentBuffer[5];
                }
                if (this.d == null) {
                    this.d = new double[3];
                }
                int a = segment.a(this.c);
                if (a > 0) {
                    for (int i = 0; i < a; i++) {
                        b(this.c[i].get());
                        if (this.k) {
                            return true;
                        }
                    }
                } else {
                    b(segment);
                    if (this.k) {
                        return true;
                    }
                }
            }
        }
        return false;
    }

    private static int a(Polygon polygon, Point2D point2D, double d) {
        PointInPolygonHelper pointInPolygonHelper = new PointInPolygonHelper(true, point2D, d);
        SegmentIteratorImpl querySegmentIterator = ((MultiPathImpl) polygon._getImpl()).querySegmentIterator();
        while (querySegmentIterator.nextPath()) {
            while (querySegmentIterator.hasNextSegment()) {
                if (pointInPolygonHelper.c(querySegmentIterator.nextSegment())) {
                    return -1;
                }
            }
        }
        return pointInPolygonHelper.a();
    }

    public static int isPointInPolygon(Polygon polygon, Point2D point2D, double d) {
        RasterizedGeometry2D rasterizedGeometry;
        if (polygon.isEmpty()) {
            return 0;
        }
        Envelope2D envelope2D = new Envelope2D();
        polygon.a(envelope2D);
        envelope2D.inflate(d, d);
        if (envelope2D.contains(point2D)) {
            GeometryAccelerators _getAccelerators = ((MultiPathImpl) polygon._getImpl())._getAccelerators();
            if (_getAccelerators != null && (rasterizedGeometry = _getAccelerators.getRasterizedGeometry()) != null) {
                RasterizedGeometry2D.HitType queryPointInGeometry = rasterizedGeometry.queryPointInGeometry(point2D.x, point2D.y);
                if (queryPointInGeometry == RasterizedGeometry2D.HitType.INSIDE) {
                    return 1;
                }
                if (queryPointInGeometry == RasterizedGeometry2D.HitType.OUTSIDE) {
                    return 0;
                }
            }
            return a(polygon, point2D, d);
        }
        return 0;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public static int a(Polygon polygon, double d, double d2, double d3) {
        RasterizedGeometry2D rasterizedGeometry;
        if (polygon.isEmpty()) {
            return 0;
        }
        Envelope2D envelope2D = new Envelope2D();
        polygon.a(envelope2D);
        envelope2D.inflate(d3, d3);
        if (envelope2D.contains(d, d2)) {
            GeometryAccelerators _getAccelerators = ((MultiPathImpl) polygon._getImpl())._getAccelerators();
            if (_getAccelerators != null && (rasterizedGeometry = _getAccelerators.getRasterizedGeometry()) != null) {
                RasterizedGeometry2D.HitType queryPointInGeometry = rasterizedGeometry.queryPointInGeometry(d, d2);
                if (queryPointInGeometry == RasterizedGeometry2D.HitType.INSIDE) {
                    return 1;
                }
                if (queryPointInGeometry == RasterizedGeometry2D.HitType.OUTSIDE) {
                    return 0;
                }
            }
            return a(polygon, new Point2D(d, d2), d3);
        }
        return 0;
    }

    public static int isPointInRing(MultiPathImpl multiPathImpl, int i, Point2D point2D, double d) {
        Envelope2D envelope2D = new Envelope2D();
        multiPathImpl.queryLooseEnvelope2D(envelope2D);
        envelope2D.inflate(d, d);
        if (envelope2D.contains(point2D)) {
            PointInPolygonHelper pointInPolygonHelper = new PointInPolygonHelper(true, point2D, d);
            SegmentIteratorImpl querySegmentIterator = multiPathImpl.querySegmentIterator();
            querySegmentIterator.resetToPath(i);
            if (querySegmentIterator.nextPath()) {
                while (querySegmentIterator.hasNextSegment()) {
                    if (pointInPolygonHelper.c(querySegmentIterator.nextSegment())) {
                        return -1;
                    }
                }
            }
            return pointInPolygonHelper.a();
        }
        return 0;
    }

    public static int isPointInPolygon(Polygon polygon, Point point, double d) {
        if (point.isEmpty()) {
            return 0;
        }
        return isPointInPolygon(polygon, point.a(), d);
    }

    public static int isPointInAnyOuterRing(Polygon polygon, Point2D point2D, double d) {
        Envelope2D envelope2D = new Envelope2D();
        polygon.a(envelope2D);
        envelope2D.inflate(d, d);
        if (envelope2D.contains(point2D)) {
            PointInPolygonHelper pointInPolygonHelper = new PointInPolygonHelper(false, point2D, d);
            MultiPathImpl multiPathImpl = (MultiPathImpl) polygon._getImpl();
            SegmentIteratorImpl querySegmentIterator = multiPathImpl.querySegmentIterator();
            while (querySegmentIterator.nextPath()) {
                if (!(multiPathImpl.calculateRingArea2D(querySegmentIterator.getPathIndex()) < Utils.DOUBLE_EPSILON)) {
                    while (querySegmentIterator.hasNextSegment()) {
                        if (pointInPolygonHelper.c(querySegmentIterator.nextSegment())) {
                            return -1;
                        }
                    }
                    continue;
                } else {
                    querySegmentIterator.resetToLastSegment();
                    while (querySegmentIterator.hasPreviousSegment()) {
                        Segment previousSegment = querySegmentIterator.previousSegment();
                        previousSegment.l();
                        if (pointInPolygonHelper.c(previousSegment)) {
                            return -1;
                        }
                    }
                    continue;
                }
            }
            return pointInPolygonHelper.a();
        }
        return 0;
    }
}
